package com.ld.security.common.shiro.configuration;

import com.ld.security.common.shiro.Interceptor.BaseInterceptor;
import com.ld.security.common.shiro.config.ShiroProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Configuration
@ConditionalOnClass({WebMvcConfigurer.class})
public class AuthConfiguration implements WebMvcConfigurer {

    @Autowired
    List<BaseInterceptor> baseInterceptorList;
    @Autowired
    ShiroProperties shiroProperties;


    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        HashMap<String,BaseInterceptor> map = new HashMap<>();

        for (BaseInterceptor baseInterceptor : baseInterceptorList) {
            map.put(baseInterceptor.getName(),baseInterceptor);
        }
      List<String> filterUrls = shiroProperties.getFilter().getFilterUrl();
        for (String filterUrl : filterUrls) {
            String[] strs = filterUrl.split("->");
            String[] xs=strs[0].replace(" ","").split(",");
            for (String s : xs) {
                registry.addInterceptor(map.get(s)).addPathPatterns(strs[1]);

            }
        }


    }




}